package com.hrms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.entity.Customer;
import com.hrms.mapper.CustomerMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    public boolean add(Customer customer) {
        // 在表customer中添加一条记录
        // true表示添加成功
        return save(customer);
    }

    public boolean updatePhone(Customer customer) {
        // 根据customer_id修改customer_phone
        // true表示修改成功
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customer.getCustomer_id());

        return update(customer, wrapper);
    }

    public String selectCustomerName(Customer customer) {
        // 根据customer_id返回customer_name
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customer.getCustomer_id())
                .select("customer_name");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("customer_name").toString();
    }

    public String selectCustomerPhone(Customer customer) {
        // 根据customer_id返回customer_phone
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customer.getCustomer_id())
                .select("customer_phone");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("customer_phone").toString();
    }

    public String selectCustomerSex(Customer customer) {
        // 根据customer_id返回customer_sex
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customer.getCustomer_id())
                .select("customer_sex");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("customer_sex").toString();
    }
}
